﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "CmdImport.h"
#include "CommandIds.h"
#include "CommandRegister.h"
#include "UiCommonDialog.h"
#include "GbmpFileSystem.h"
#include "FilePath.h"
#include "IUserTransaction.h"
#include "UiDocumentViewUtils.h"
#include "IFileImporter.h"
#include "ModuleUtils.h"
#include "IUiDocumentViewManager.h"
#include "IModelView.h"
#include "IUiView.h"
#include "IFileImporterOptions.h"
#include "IGraphicsPlane.h"
#include "ICanvas.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

using namespace Sample;
using namespace gcmp;

REGISTER_COMMAND(CmdImportDWG);
CmdImportDWG::CmdImportDWG() :CommandBase(ID_CMD_IMPORT_DWG, true)
{

}
CmdImportDWG::~CmdImportDWG()
{

}

bool CmdImportDWG::IsEnabled() const
{
    return UiDocumentViewUtils::GetCurrentDocument() != nullptr;
}

bool CmdImportDWG::IsVisible() const
{
    return UiDocumentViewUtils::GetCurrentDocument() != nullptr;
}

OwnerPtr<IAction> CmdImportDWG::ExecuteCommand(const CommandParameters& cmdParams)
{
    //0-动态加载导入模块
    HMODULE hModule = ModuleUtils::LoadDwgModule();
    DBG_WARN_AND_RETURN_UNLESS(hModule, 0, L"加载dwg模块失败!", L"GDMP", L"2023-09-30");

    //1-
    std::wstring filePathName = UiCommonDialog::ShowOpenFileDialog(GBMP_TR(L"打开DWG文件"), gcmp::FileSystem::GetWorkingDirPath(), L"*.dwg");
    if (filePathName.empty())
    {
        return nullptr;
    }
    std::wstring m_filePassword = L"";
    OwnerPtr<IFileImporter> opFileImporter = IFileImporter::Create(filePathName, m_filePassword);
    opFileImporter->SetFileImporterOptions(IFileImporterOptions::Create(L".dwg"));

    DBG_WARN_AND_RETURN_UNLESS(opFileImporter->ReadFile() && !opFileImporter->IsFileEmpty(), 0,
        L"加载文件失败", L"GDMP", L"2023-05-30");
    //2-
    IUiView* currentView = IUiDocumentViewManager::Get()->GetCurrentUiView();
    DBG_WARN_AND_RETURN_UNLESS(currentView != nullptr, nullptr, L"CurrentView为空", L"GDMP", L"2023-09-30");
    IModelView* pCurrentModelView = currentView->GetModelView();
    IDocument* pDoc = pCurrentModelView->GetDocument();

    OwnerPtr<IUserTransaction> opUsertransaction = IUserTransaction::Create(pDoc, L"导入dwg文件");
    {
        int ymp = 0;
        std::vector<int> layerIds;
        layerIds.emplace_back(ymp);
        opFileImporter->SetLayersToImport(layerIds);

        std::wstring m_errorMessage;
        ElementId m_importID = opFileImporter->ImportFile(pDoc, pCurrentModelView, m_errorMessage);

        OwnerPtr<IGraphicsPlane> opGPlane = IGraphicsPlane::CreateByNormal(Vector3d(0, 0, 0), Vector3d(0, 0, 1));
        bool issucced = opFileImporter->AutoAlign(opGPlane.get());
        if (issucced)
        {
            opUsertransaction->Commit();
            ICanvas* pCanvas = currentView->GetCanvas();
            pCanvas->Refresh();
        }
        else
        {
            opUsertransaction->Rollback();
        }
    }
    return nullptr;
}

